/**
 *
 * 时间戳 to 时间
 */
let timestampToTime = timestamp => {
  if (timestamp == null) return '-'
  let date = new Date(timestamp)
  let Y = date.getFullYear() + '-'
  let M =
    (date.getMonth() + 1 < 10
      ? '0' + (date.getMonth() + 1)
      : date.getMonth() + 1) + '-'
  let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
  let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
  let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
  let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
  return Y + M + D + h + m + s
}
let downloadFile = (fileJson) => {
  let data = JSON.parse(fileJson.data)
  // let url = data.url
  // let name = data.name
  let url = "http://localhost:8080/file/download/"
  let name = "temp.zip"
  if (url.indexOf('.txt') != -1) {
    downloadFile_txt_getUrl(url, name)
    return
  }
  let link = document.createElement('a')
  link.style.display = 'none'
  link.href = url
  document.body.appendChild(link)
  link.click()
}
let download = (name, data) => {
  var export_blob = new Blob([data]);
  var urlObject = window.URL || window.webkitURL || window;
  var save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a')
  save_link.href = urlObject.createObjectURL(export_blob);
  save_link.download = name;
  save_link.click();
}

let fileSizeToStr = (size) => {
  if (size == null) return '-'
  return fileSizeToStr_(size, 0)
}

function fileSizeToStr_ (size, n) {
  if (n > 3) return 0;
  if (n == 0 && size < 1000) {
    return size + 'B'
  }
  if (size < 1000) {
    let unit = ['KB', 'MB', 'GB']
    return Math.round(size * 100) / 100 + unit[n - 1]
  }
  return fileSizeToStr_(size / 1000, n + 1)
}
//如果下载的文件是 txt，浏览器会自动打开而不是下载。
let downloadFile_txt_getUrl = (url, name) => {
  const xmlHttp = new XMLHttpRequest()
  xmlHttp.open('get', url, true)
  xmlHttp.send()
  xmlHttp.onreadystatechange = () => {
    if (xmlHttp.readyState == 4) {
      //4表示执行完成
      if (xmlHttp.status == 200) {
        download(name, xmlHttp.responseText)
      }
    }
  }
}
let checkFileType = (name) => {
  if (name.charAt(name.length - 1) == '/') {
    return '文件夹'
  }
  let f = name.substr(name.indexOf('.') + 1)
  if (f == 'jpg' || f == 'png' || f == 'gif' || f == 'bmp' || f == 'jpeg') {
    return '图片'
  } else if (f == 'txt' || f == 'md') {
    return '文本'
  } else if (f == 'mp3') {
    return '音频'
  } else if (f == 'mp4') {
    return '视频'
  } else {
    return '文件'
  }
}


let copyToClipboard = (text) => {

  const el = document.createElement('textarea');
  el.value = text;
  el.setAttribute('readonly', '');
  el.style.position = 'absolute';
  el.style.left = '-9999px';
  document.body.appendChild(el);
  const selected =
    document.getSelection().rangeCount > 0 ? document.getSelection().getRangeAt(0) : false;
  el.select();
  document.execCommand('copy');
  document.body.removeChild(el);
  if (selected) {
    document.getSelection().removeAllRanges();
    document.getSelection().addRange(selected);
  }

}
let UUID = () => {
  var d = new Date().getTime();
  if (window.performance && typeof window.performance.now === "function") {
    d += performance.now(); //use high-precision timer if available
  }
  var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    var r = (d + Math.random() * 16) % 16 | 0;
    d = Math.floor(d / 16);
    return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
  });
  return uuid;
}
export default {
  UUID,
  timestampToTime,
  downloadFile,
  fileSizeToStr,
  download,
  downloadFile_txt_getUrl,
  checkFileType,
  copyToClipboard
}